/**
 * banner管理初始化
 */
var Banner = {
    id: "BannerTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Banner.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '记录编号', field: 'id', visible: true, align: 'center', valign: 'middle'},
            {title: 'BANNER标签名称', field: 'bannerLabel', visible: true, align: 'center', valign: 'middle'},
            {title: 'banner图片', img:true, field: 'bannerImgFilePath', visible: true, align: 'center', valign: 'middle'
                ,formatter:function(value, row, index){
                return "<img style='width: 100px;height: 100px' src='"+value+"'>";
            } },
           /* {title: 'banner跳转Url', field: 'bannerUrl', visible: true, align: 'center', valign: 'middle'},*/
            {title: '产品名称', field: 'prdName', visible: true, align: 'center', valign: 'middle'},
            {title: '公司名称', field: 'comName', visible: true, align: 'center', valign: 'middle'},
            {title: '排序编号', field: 'bannerSeq', visible: true, align: 'center', valign: 'middle'},
           /* {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle'},*/
            {title: '创建时间', field: 'createtime', visible: true, align: 'center', valign: 'middle'},
            {title: '维护人', field: 'maintainer', visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
Banner.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Banner.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加banner
 */
Banner.openAddBanner = function () {
    var index = layer.open({
        type: 2,
        title: '添加banner',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/banner/banner_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看banner详情
 */
Banner.openBannerDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: 'banner详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/banner/banner_update/' + Banner.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除banner
 */
Banner.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/banner/delete", function (data) {
            Feng.success("删除成功!");
            Banner.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("bannerId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询banner列表
 */
Banner.search = function () {
    var queryData = {};
    queryData['comId'] = $("#comId").val();
    queryData['prdId'] = $("#prdId").val();
    Banner.table.refresh({query: queryData});
};

function getProdutList4ComId(comId) {
    $("#prdId").html('');
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/busPrductInfo/list4ComId", function(data){

        var optionHtml='<option value="">请选择产品</option>';
        for(var i=0;i<data.length;i++){
            optionHtml+='<option value="'+data[i].id+'">'+data[i].prdName+'</option>';
        }
        $("#prdId").html(optionHtml);
    },function(data){
        Feng.error("获取产品失败!" + data.responseJSON.message + "!");
    });
    ajax.set("comId",comId);
    ajax.start();
}
function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentDeptMenu" || $(
            event.target).parents("#parentDeptMenu").length > 0)) {
        Banner.hideDeptSelectTree();
    }
}
/**
 * 显示部门选择的树
 *
 * @returns
 */
Banner.showDeptSelectTree = function() {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentDeptMenu").css({
        left : pNameOffset.left + "px",
        top : pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
Banner.hideDeptSelectTree = function() {
    $("#parentDeptMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}
/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
Banner.onClickDept = function(e, treeId, treeNode) {
    $("#pName").attr("value", Banner.zTreeInstance.getSelectedVal());
    $("#comId").attr("value", treeNode.id);
    getProdutList4ComId(treeNode.id);
}
$(function () {
    var defaultColunms = Banner.initColumn();
    var table = new BSTable(Banner.id, "/banner/list", defaultColunms);
    table.setPaginationType("client");
    Banner.table = table.init();

    var ztree = new $ZTree("parentDeptMenuTree", "/dept/tree");
    ztree.bindOnClick(Banner.onClickDept);
    ztree.init();
    Banner.zTreeInstance = ztree;
});
